package com.sduept.nwld.dataserver.controller.mobiledata;

import com.sduept.bigdata.web.core.manager.NoticeManager;
import com.sduept.bigdata.web.core.model.NoticeAnnouncement;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每天0点定时检测未发布的公告
 */
@Service
public class NoticePublishTimerSchedule {
    @Autowired
    private NoticeManager nm;

    //	@Schedule(hour = "0", minute = "0", second = "0")
    @Scheduled(cron = "0 0 0 * * ?")
    public void noticePublishTimer() {
        Calendar calendar = Calendar.getInstance();
        //查找未发布的公告中，发布时间等于今天的公告，然后置为发布
        Date time = calendar.getTime();
        String stimeStr = DateUtil.getStringDate(time, DateUtil.pattern_ymd000);
        String etimeStr = DateUtil.getStringDate(time, DateUtil.pattern_ymd235959);
        Date sDate = DateUtil.getDateByString(stimeStr, DateUtil.pattern_ymdhms);
        Date eDate = DateUtil.getDateByString(etimeStr, DateUtil.pattern_ymdhms);
        List<NoticeAnnouncement> notices = nm.getUnPublishedNoticesByPublishDate(sDate, eDate);
        for (NoticeAnnouncement na : notices) {
            na.setPublish(true);
            nm.createOrUpdateNotice(na);
        }
    }


}
